package com.apchernykh.player;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RemoteViews;
import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.rozhlasapp.RozhlasAppActivity;
import com.apchernykh.rozhlasapp.services.BackgroundPlaybackService;
import com.apchernykh.rozhlasapp.utils.ConditionalLog;
import com.apchernykh.rozhlasapp.utils.PreferencesCache;
import com.apchernykh.types.ApplicationModel.EPlaybackStatuses;

public class RozhlasWidgetProvider extends AppWidgetProvider {
	static EPlaybackStatuses mCurrentlyPlaying = EPlaybackStatuses.STOPPED;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.appwidget.AppWidgetProvider#onUpdate(android.content.Context,
	 * android.appwidget.AppWidgetManager, int[])
	 */
	@Override
	public void onUpdate(Context iContext, AppWidgetManager iWidgetManager,
			int[] iWidgetIdsArray) {
		ConditionalLog.d("onUpdate");
		try {
			PreferencesCache aPref = new PreferencesCache();
			aPref.refresh(iContext);
			updateDisplayState(iContext, reCreateView(iContext, aPref));
		} catch (Exception e) {
			ConditionalLog.e(this.toString(), e.getMessage(), e);
		}
		super.onUpdate(iContext, iWidgetManager, iWidgetIdsArray);
	}

	private static RemoteViews reCreateView(Context iContext, PreferencesCache iPreferencesCache) {
		return resetView(iContext, new RemoteViews(iContext.getPackageName(),
				R.layout.station_simple_widget), iPreferencesCache);
	}

	public static void updateDisplayState(Context iContext, RemoteViews iRemoteView) {
		ComponentName aThisWidget = new ComponentName(iContext,
				RozhlasWidgetProvider.class);
		AppWidgetManager.getInstance(iContext).updateAppWidget(aThisWidget,
				iRemoteView);
	}

	public static RemoteViews resetView(Context iContext, RemoteViews iRemoteView, PreferencesCache iPreferencesCache) {


		switch (mCurrentlyPlaying) {
		case STOPPED:
			setStationInfo(iContext, iRemoteView, iPreferencesCache, false);
			break;
		case PLAYING:
			setStationInfo(iContext, iRemoteView, iPreferencesCache, true);
			break;
		default:
			setDefaultStationInfo(iContext, iRemoteView, iPreferencesCache);
			break;
		}
		return iRemoteView;
	}

	private static void setDefaultStationInfo(Context iContext,
			RemoteViews iRemoteView, PreferencesCache iPreferencesCache) {
		iRemoteView.setTextViewText(R.id.title_one_line,
				iPreferencesCache.mLastStationName);

		Intent aNotificationIntent = new Intent(iContext.getApplicationContext(),
				RozhlasAppActivity.class);
		PendingIntent aPendingIntent = PendingIntent.getActivity(iContext.getApplicationContext(), 0,
				aNotificationIntent, 0);

		iRemoteView.setOnClickPendingIntent(R.id.top_layout, aPendingIntent);
		iRemoteView.setOnClickPendingIntent(R.id.station_logo, aPendingIntent);		
		iRemoteView.setViewVisibility(R.id.station_logo, View.GONE);
	}

	public static void setStationInfo(Context iContext, RemoteViews iRemoteView,
			PreferencesCache iPreferencesCache, boolean iIsPlayingNow) {
		iRemoteView.setTextViewText(R.id.title_one_line,
				iPreferencesCache.mLastStationName);

		PendingIntent aPlaybackControlIntent = BackgroundPlaybackService
				.createBackgroundPlaybackControlIntent(iContext,
						!iIsPlayingNow);

		iRemoteView.setOnClickPendingIntent(R.id.top_layout, aPlaybackControlIntent);
		iRemoteView.setOnClickPendingIntent(R.id.station_logo, aPlaybackControlIntent);
		iRemoteView.setViewVisibility(R.id.station_logo, View.VISIBLE);
		
		if (iIsPlayingNow) {
			iRemoteView.setImageViewResource(R.id.station_logo, R.drawable.btn_playback_ic_pause_small);
		} else {
			iRemoteView.setImageViewResource(R.id.station_logo, R.drawable.ic_menu_play_clip);
		}
	}

	public static void onPlaybackStatusChange(
			EPlaybackStatuses iPlaybackStatus, Context iContext, PreferencesCache iPreferencesCache) {
		if(mCurrentlyPlaying != iPlaybackStatus)
		{
			mCurrentlyPlaying = iPlaybackStatus;
			updateDisplayState(iContext, reCreateView(iContext, iPreferencesCache));
		}
	}


	@Override
	public void onReceive(Context iContext, Intent iIntent) {
		ConditionalLog.mLogger.mLoggerFileName = "Android/"
				+ iContext.getString(R.string.app_name) + ".log";
		ConditionalLog.mLogger.open();
		super.onReceive(iContext, iIntent);
	}
}
